package com.xmad.ksadutils.AdSDK.AdMold.Pangolin;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBase;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;
import com.xmad.ksadutils.AdSDK.Utlis.Tool;

import java.util.HashMap;
import java.util.Map;

public class PGloadFullScreenVideoAd2 extends AdBase {
    private volatile static PGloadFullScreenVideoAd2 mInstance;
    TTFullScreenVideoAd Ad;
    private TTAdNative mTTAdNative;
    static Map<String, AdComplete> mMap= new HashMap<String, AdComplete>();
    /**
     * 单例模式
     *
     * @return
     */
    public static PGloadFullScreenVideoAd2 getInstance() {
        if (mInstance == null) {
            synchronized (PGloadFullScreenVideoAd2.class) {
                if (mInstance == null) {
                    mInstance = new PGloadFullScreenVideoAd2();
                }
            }
        }
        return mInstance;
    }


    @Override
    protected void load() {
        if (getPGid()==null||getPGid().length==0){
            Log.e(TAG,"先设置梯度ID");
            return;
        }
        //新建
        NewMap();
        //获取广告对象List列表
        GetAdList();
    }

    //新建
    public void NewMap(){
        Log.e(TAG,mMap.toString());
        if (mMap.size()==0){
            for (String Id:getPGid()){
                mMap.put(Id,null);
            }
        }
    }

    //获取广告对象List列表
    public void GetAdList(){
        for(String key:mMap.keySet()){
            if (mMap.get(key)==null||mMap.get(key).getAd()==null){
                getAds(key);
            }
        }
    }

    private void   getAds(String id){
        Log.e(TAG,"PGloadFullScreenVideoAd2加载"+ id);
        mTTAdNative= TTAdManagerHolder.get().createAdNative(activity);
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(id)
                .build();
        //step5:请求广告
        mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener(){
            @Override
            public void onError(int code, String msg) {
                Log.e(TAG, "Callback --> onError: " + code + ", " + String.valueOf(msg));
                if (listener!=null){
                    listener.onError(code+msg);
                }
            }
            @Override
            public void onFullScreenVideoAdLoad(final TTFullScreenVideoAd ad) {
                Log.d(TAG, "Callback --> onFullScreenVideoAdLoad");
                Ad = ad;
                Ad.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {

                    @Override
                    public void onAdShow() {
                        if (listener!=null){
                            listener.onAdShow();
                        }
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        Log.e(TAG, "onAdVideoBarClick");
                        if (listener!=null){
                            listener.onAdClicked();
                        }
                    }

                    @Override
                    public void onAdClose() {
                        Log.e(TAG, "onAdClose");
                        if (listener!=null){
                            listener.close();
                        }
                    }

                    @Override
                    public void onVideoComplete() {
                        Log.e(TAG, "onVideoComplete");
                        if (listener!=null){
                            listener.onComplete();
                        }
                    }

                    @Override
                    public void onSkippedVideo() {
                        Log.e(TAG, "onSkippedVideo");
                        if (listener!=null){
                            listener.onJump(200);
                        }
                    }

                });
                if (Ad != null) {
                    //展示广告，并传入广告展示的场景
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            Ad.showFullScreenVideoAd(activity);
                        }
                    });
                } else {
                    Log.e(TAG,"请先加载广告");
                }

                ad.setDownloadListener(new TTAppDownloadListener() {
                    @Override
                    public void onIdle() {
                    }

                    @Override
                    public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadActive==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);


                    }

                    @Override
                    public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadPaused===totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFailed==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFinished==totalBytes=" + totalBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onInstalled(String fileName, String appName) {
                        Log.d("DML", "onInstalled==" + ",fileName=" + fileName + ",appName=" + appName);

                    }
                });
            }

            @Override
            public void onFullScreenVideoCached() {
                Log.e(TAG, "Callback --> onFullScreenVideoCached");

            }

            @Override
            public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {
                Log.e(TAG, "Callback --> onFullScreenVideoCached");

            }
        });

    }

    @Override
    protected void start() {

    }

    public class AdComplete{
        private boolean Complete;
        private TTFullScreenVideoAd ad;

        public AdComplete(boolean complete, TTFullScreenVideoAd ad) {
            Complete = complete;
            this.ad = ad;
        }

        public boolean isComplete() {
            return Complete;
        }

        public void setComplete(boolean complete) {
            Complete = complete;
        }

        public TTFullScreenVideoAd getAd() {
            return ad;
        }

        public void setAd(TTFullScreenVideoAd ad) {
            this.ad = ad;
        }
    }

}


